package com.opensky.qatest.ui.page;

import java.util.List;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.util.common.AssertReport;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.HTML;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "? page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class CuratorPickerPage extends LoadableComponent<CuratorPickerPage> implements IPage {

    // ************************ Locators ************************

    private static final String CURATOR_PICKER_PAGE_URL = "https://opensky.com/curator-picker";

    private static final String PERSONALIZE_STEP = "//header//li[@class='active']/span[text()='Personalize']";

    private static final String CONTINUE_BTN = "//div[@class='footer-wrap']//input[@value='Continue']";

    // private static final String CATEGORY_LIST_ITEMS =
    // "//ul[@class='categories']/li";

    private static final String RECCOMENDED_CATEGORY = "//ul[@class='categories']/li[contains(., 'Recommended')]";

    private static final String FOOD_CATEGORY = "//ul[@class='categories']/li[contains(., 'Food')]";

    private static final String HEALTH_CATEGORY = "//ul[@class='categories']/li[contains(., 'Health')]";

    private static final String HOME_CATEGORY = "//ul[@class='categories']/li[contains(., 'Home')]";

    private static final String STYLE_CATEGORY = "//ul[@class='categories']/li[contains(., 'Style')]";

    private static final String EMAILS_CATEGORY = "//ul[@class='categories']/li[contains(., 'OpenSky Emails')]";

    private static final String YOUR_CURATORS_TITLE = "//div[@class='aside-block following-list']/h3[text()='Your Curators']";

    private static final String SLOT_LIST_ITEM = "//div[@class='aside-block following-list']/ul[@class='slots curators']/li";

    private static final String EMPTY_SLOT = "//div[@class='aside-block following-list']/ul[@class='slots curators']/li[@class='curator empty-slot']";

    private static final String CURATOR_SLOT = "//div[@class='aside-block following-list']/ul[@class='slots curators']/li[@class='curator']";

    private static final String CURATOR_SLOT_UNFOLLOW_BTN = "//div[@class='aside-block following-list']/ul[@class='slots curators']/li[@class='curator']//input[@name='unfollow']";

    private static final String CURATOR_LIST = "//div[@class='seller-picker']//ul";

    private static final String CURATOR_BOX = "//div[@class='seller-picker']//ul/li[1]";

    private static final String DEFAULT_PICKER = "//div[@class='seller-picker']/div[1]/div[@class='category-header' and contains(., 'Recommendations from OpenSky')]";

    private static final String CURATOR_AVATAR = "//div[@class='seller-picker']//ul/li[1]/div[@class='avatar']/img";

    private static final String CURATOR_NAME = "//div[@class='seller-picker']//ul/li[1]//div[@class='bio']//h3";

    private static final String CURATOR_PERSONAL_TAG = "//div[@class='seller-picker']//ul/li[1]//div[@class='bio']//h4";

    private static final String CURATOR_RECENT_SALE_LIST = "//div[@class='seller-picker']//ul/li[1]//div[@class='offers']//div[@class='recent']";

    private static final String CURATOR_RECENT_SALE = "//div[@class='seller-picker']//ul/li[1]//div[@class='offers']//div[@class='recent']//ul/li/a";

    private static final String FOLLOW_BTN = "//div[@class='seller-picker']//ul/li[1]//a[text()='Follow']";

    private static final String UNFOLLOW_BTN = "//div[@class='seller-picker']//ul/li[1]//input[@value='unfollow']";

    private static final String FOLLOWERS = "//div[@class='seller-picker']//ul/li[1]//span[@class='followers']";

    private static final String FOLLOWING_MESSAGE = "//div[@class='seller-picker']//ul/li[1]//div[contains(., 'You are following')]";

    // private static final String ? = "";
    // private static final String ? = "";
    // private static final String ? = "";
    // private static final String ? = "";

    // ************************ Complex page objects **************************

    private final WebDriverWrapper driver;

    public CuratorPickerPage(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(CURATOR_PICKER_PAGE_URL);
    }

    public OfferPage openCuratorRecentSale() {

	WebElement productLink = driver.waitAndGetElement("CURATOR_RECENT_SALE", By.xpath(CURATOR_RECENT_SALE));

	String href = productLink.getAttribute(HTML.HREF);
	String curator = href.split("/")[3];
	String product = href.split("/")[5];

	OfferPage offerPage = new OfferPage(driver, curator, product);

	driver.get(href);

	if (offerPage.isPageLoaded()) {
	    return offerPage;
	} else {
	    return null;
	}
    }

    public void clickFollowCuratorBtn() {
	driver.waitAndSendEnter("FOLLOW_BTN", By.xpath(FOLLOW_BTN));
    }

    public void clickUnfollowCuratorBtn() {
	driver.waitAndSendEnter("UNFOLLOW_BTN", By.xpath(UNFOLLOW_BTN));
    }

    public void clickUnfollowCuratorBtnFromLeftNav() {
	driver.waitAndSendEnter("CURATOR_SLOT_UNFOLLOW_BTN", By.xpath(CURATOR_SLOT_UNFOLLOW_BTN));
    }

    public EmailsPage openOpenskyEmails() {

	EmailsPage emailsPage = new EmailsPage(driver);

	driver.waitAndClick("EMAILS_CATEGORY", By.xpath(EMAILS_CATEGORY));

	if (emailsPage.isPageLoaded()) {
	    return emailsPage;
	} else {
	    return null;
	}
    }

    public InvitePage clickContinueBtn() {

	InvitePage invitePage = new InvitePage(driver);

	driver.waitAndSendEnter("CONTINUE_BTN", By.xpath(CONTINUE_BTN));

	if (invitePage.isPageLoaded()) {
	    return invitePage;
	} else {
	    return null;
	}
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() throws Error {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(AssertReport.wrongURL(CURATOR_PICKER_PAGE_URL, url),
		LogicUtils.isURLEqual(CURATOR_PICKER_PAGE_URL, url));
    }

    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(CURATOR_PICKER_PAGE_URL);
    }

    public boolean isBaseElementsPresent() {
	driver.waitAndCheckElementPresent("PERSONALIZE_STEP", By.xpath(PERSONALIZE_STEP));
	driver.waitAndCheckElementPresent("CONTINUE_BTN", By.xpath(CONTINUE_BTN));
	driver.waitAndCheckElementPresent("RECCOMENDED_CATEGORY", By.xpath(RECCOMENDED_CATEGORY));
	driver.waitAndCheckElementPresent("FOOD_CATEGORY", By.xpath(FOOD_CATEGORY));
	driver.waitAndCheckElementPresent("HEALTH_CATEGORY", By.xpath(HEALTH_CATEGORY));
	driver.waitAndCheckElementPresent("HOME_CATEGORY", By.xpath(HOME_CATEGORY));
	driver.waitAndCheckElementPresent("STYLE_CATEGORY", By.xpath(STYLE_CATEGORY));
	driver.waitAndCheckElementPresent("EMAILS_CATEGORY", By.xpath(EMAILS_CATEGORY));
	driver.waitAndCheckElementPresent("YOUR_CURATORS_TITLE", By.xpath(YOUR_CURATORS_TITLE));
	driver.waitAndCheckElementPresent("EMPTY_SLOT", By.xpath(EMPTY_SLOT));
	return true;
    }

    public boolean isCuratorsPickerListBaseElementsPresent() {
	driver.waitAndCheckElementPresent("CURATOR_LIST", By.xpath(CURATOR_LIST));
	driver.waitAndCheckElementPresent("DEFAULT_PICKER", By.xpath(DEFAULT_PICKER));
	return true;
    }

    public boolean isCuratorBaseElementsPresent() {
	driver.waitAndCheckElementPresent("CURATOR_LIST", By.xpath(CURATOR_LIST));
	driver.waitAndCheckElementPresent("CURATOR_AVATAR", By.xpath(CURATOR_AVATAR));
	driver.waitAndCheckElementPresent("CURATOR_NAME", By.xpath(CURATOR_NAME));
	driver.waitAndCheckElementPresent("CURATOR_PERSONAL_TAG", By.xpath(CURATOR_PERSONAL_TAG));
	driver.waitAndCheckElementPresent("CURATOR_RECENT_SALE_LIST", By.xpath(CURATOR_RECENT_SALE_LIST));
	driver.waitAndCheckElementPresent("FOLLOW_BTN", By.xpath(FOLLOW_BTN));
	driver.waitAndCheckElementPresent("FOLLOWERS", By.xpath(FOLLOWERS));
	return true;
    }

    public boolean isFollowCuratorReactCorrect() {

	driver.waitAndCheckElementPresent("UNFOLLOW_BTN", By.xpath(UNFOLLOW_BTN));
	driver.waitAndCheckElementPresent("FOLLOWING_MESSAGE", By.xpath(FOLLOWING_MESSAGE));

	WebElement curatorBox = driver.waitAndGetElement("CURATOR_BOX", By.xpath(CURATOR_BOX));

	if (!curatorBox.getAttribute(HTML.CLASS).contains("following")) {
	    return false;
	}

	List<WebElement> slots = driver.waitAndGetElements("SLOT_LIST_ITEM", By.xpath(SLOT_LIST_ITEM));

	String curatorInList = driver.waitAndGetElement("CURATOR_BOX", By.xpath(CURATOR_BOX)).getAttribute(
		"data-curator-slug");
	String curatorInSlot = slots.get(0).getAttribute("data-curator-slug");

	if (!curatorInList.equals(curatorInSlot)) {
	    return false;
	}

	return true;
    }

    public boolean isUnfollowCuratorReactCorrect() {

	driver.waitAndCheckElementNotPresent("CURATOR_SLOT", By.xpath(CURATOR_SLOT));
	driver.waitAndCheckElementPresent("EMPTY_SLOT", By.xpath(EMPTY_SLOT));
	return true;
    }

    // ************************ GET/SET methods **************************

}
